﻿Imports System
Imports System.IO
Imports TBCC.UtilityMobile

Public Class ProjectService

    'Private Shared _projectConfigFile As String

    Shared Sub New()

    End Sub

    'revised
    Public Shared Function CreateBasicInfo(ByVal pi As ProjectItem) As Boolean
        Dim pid_str As String
        pid_str = Trim(pi.ProjectID).Replace("-", "_")
        Dim strSql As String
        Dim tic As New TBCC.UtilityMobile.TranstrationItem

        strSql = "Insert Into tbccProject(ProjectID,ProjectName,listNo,remark,phoneNumber,ConnectType," _
                & "ComNumber,DevCount,RefCount,imageCount,AuthCode,ProjectType,ProjectMacID) Values(?p0,?p1,?p2,?p3,?p4,?p5,?p6,?p7,?p8,?p9,?p10,?p11,?p12)"

        Dim p(12) As MySql.Data.MySqlClient.MySqlParameter
        For i As Integer = 0 To p.GetUpperBound(0)
            p(i) = New MySql.Data.MySqlClient.MySqlParameter
            p(i).ParameterName = "p" + i.ToString
        Next

        p(0).Value = pi.ProjectID
        p(1).Value = pi.ProjectName
        p(2).Value = pi.listNo
        p(3).Value = pi.Remark
        p(4).Value = pi.Phone
        p(5).Value = pi.ConnectType
        p(6).Value = pi.ComNumber
        p(7).Value = pi.DevCount
        p(8).Value = pi.RefCount
        p(9).Value = pi.imageList.Count
        p(10).Value = pi.AuthCode
        p(11).Value = pi.ProjectType
        p(12).Value = pi.ProjectMacID
        tic.Add(strSql, p)

        '增加该工程的数据字典记录
        strSql = String.Format("Insert Into tbccDictory(projectID,key,value) Values('{0}','{1}','{2}')", pi.ProjectID, "DBBackup2Path", "")
        tic.Add(strSql, Nothing)

        strSql = String.Format("Insert Into tbccDictory(projectID,key,value) Values('{0}','{1}','{2}')", pi.ProjectID, "DBBackupTime", "00:00:00")
        tic.Add(strSql, Nothing)

        strSql = String.Format("Insert Into tbccDictory(projectID,key,value) Values('{0}','{1}','{2}')", pi.ProjectID, "AlarmSoundFile", "")
        tic.Add(strSql, Nothing)

        strSql = String.Format("Insert Into tbccDictory(projectID,key,value) Values('{0}','{1}','{2}')", pi.ProjectID, "DisableAlarmSound", "1")
        tic.Add(strSql, Nothing)

        strSql = String.Format("Insert Into tbccDictory(projectID,key,value) Values('{0}','{1}','{2}')", pi.ProjectID, "UniformityPath", "")
        tic.Add(strSql, Nothing)

        strSql = String.Format("Insert Into tbccDictory(projectID,key,value) Values('{0}','{1}','{2}')", pi.ProjectID, "UniformityTime", "")
        tic.Add(strSql, Nothing)

        '文档查询类型:1:秒,2:分钟;3:小时
        strSql = String.Format("Insert Into tbccDictory(projectID,key,value) Values('{0}','{1}','{2}')", pi.ProjectID, "DocViewType", "3")
        tic.Add(strSql, Nothing)

        strSql = String.Format("Insert Into tbccDictory(projectID,key,value) Values('{0}','{1}','{2}')", pi.ProjectID, "DocViewValue", "1")
        tic.Add(strSql, Nothing)

        'Tcp/ip心跳超时参数设置
        strSql = String.Format("Insert Into tbccDictory(projectID,key,value) Values('{0}','{1}','{2}')", pi.ProjectID, "ClientSocket_Heartbeat_IdleTime", "25")
        tic.Add(strSql, Nothing)

        strSql = String.Format("Insert Into tbccDictory(projectID,key,value) Values('{0}','{1}','{2}')", pi.ProjectID, "ClientSocket_Heartbeat_WaitTime", "5")
        tic.Add(strSql, Nothing)

        strSql = String.Format("Insert Into tbccDictory(projectID,key,value) Values('{0}','{1}','{2}')", pi.ProjectID, "ClientSocket_Heartbeat_Rep", "3")
        tic.Add(strSql, Nothing)

        '是否允许后台上传数据: 0:False,1:True
        strSql = String.Format("Insert Into tbccDictory(projectID,key,value) Values('{0}','{1}','{2}')", pi.ProjectID, "BackgroundUploadHistoryData", IIf(g_BackgroundUploadHistoryData, 1, 0))
        tic.Add(strSql, Nothing)

        strSql = String.Format("Insert Into tbccDictory(projectID,key,value) Values('{0}','{1}','{2}')", pi.ProjectID, "BackgroundUploadAlarm", IIf(g_BackgroundUploadAlarm, 1, 0))
        tic.Add(strSql, Nothing)

        '创建终端启停记录表
        strSql = String.Format("CREATE TABLE 'TbccHDStartUp_{0}'([ID] integer PRIMARY KEY ASC AUTOINCREMENT,[NetID] integer" _
                            & ",[BeginTime] datetime,[EndTime] datetime,[PageIndex] integer,[PageCount] integer,[RecordInterval] integer,[LastPageIndex] integer,[LastPageType] integer" _
                            & ",[BeginAddress] text,[EndAddress] text,[Shipper] text,[Carrier] text,[Receiver] text,[UploadStatus] integer,[LastRecordTime] datetime)", pid_str)
        tic.Add(strSql, Nothing)

        '创建终端启停记录表索引
        strSql = String.Format("CREATE  INDEX MAIN.[Index_HDStartUp_{0}] On [TbccHDStartUp_{0}] ([BeginTime] ,[EndTime] )", pid_str)
        tic.Add(strSql, Nothing)

        '创建终端启停记录明细表
        strSql = String.Format("CREATE TABLE 'TbccHDStartUpDetail_{0}'([ID] integer PRIMARY KEY ASC AUTOINCREMENT,[ParentID] integer,[NetID] integer" _
                            & ",[BeginTime] datetime,[EndTime] datetime,[UploadStatus] integer,[LastRecordTime] datetime,[LastPageIndex] integer,[LastPageType] integer" _
                            & ",[BeginAddress] text,[EndAddress] text,[Shipper] text,[Carrier] text,[Receiver] text)", pid_str)
        tic.Add(strSql, Nothing)

        '创建终端启停记录明细表索引
        strSql = String.Format("CREATE  INDEX MAIN.[Index_HDStartUpDetail_{0}] On [TbccHDStartUpDetail_{0}] ([BeginTime] ,[EndTime] )", pid_str)
        tic.Add(strSql, Nothing)

        Return SqlHelper.ExecuteTranstration(tic)

    End Function

    'revised
    Public Shared Function UpdateBasicInfo(ByVal pi As ProjectItem) As Boolean

        Dim strSql As String
        Dim p(12) As MySql.Data.MySqlClient.MySqlParameter
        Dim tic As New TBCC.UtilityMobile.TranstrationItem

        strSql = "Update tbccProject Set projectName=?,listNo=?,Remark=?,phoneNumber=?,ConnectType=?,ComNumber=?,DevCount=?,RefCount=?,ImageCount=?,AuthCode=?,ProjectType=?,ProjectMacID=? Where ProjectID=?"


        For i As Integer = 0 To p.GetUpperBound(0)
            p(i) = New MySql.Data.MySqlClient.MySqlParameter
        Next

        p(0).Value = pi.ProjectName
        p(1).Value = pi.listNo
        p(2).Value = pi.Remark
        p(3).Value = pi.Phone
        p(4).Value = pi.ConnectType
        p(5).Value = pi.ComNumber
        p(6).Value = pi.DevCount
        p(7).Value = pi.RefCount
        p(8).Value = pi.imageList.Count
        p(9).Value = pi.AuthCode
        p(10).Value = pi.ProjectType
        p(11).Value = pi.ProjectMacID
        p(12).Value = pi.ProjectID

        tic.Add(strSql, p)

        Return SqlHelper.ExecuteTranstration(tic)
    End Function

    Public Shared Function UpdatePrjDataToDb(ByVal pi As ProjectItem, ByVal netcount As Integer) As Boolean
        Dim strSql As String
        Dim pid_str As String
        Dim iTemp As Integer = 0
        pid_str = Trim(pi.ProjectID).Replace("-", "_")
        Dim tic As New TBCC.UtilityMobile.TranstrationItem

        '删除已存在的工程的数据

        '删除工程信息总表中的数据
        strSql = String.Format("Delete From tbccProject Where projectID='{0}'", pi.ProjectID)
        tic.Add(strSql, Nothing)

        '删除工程信息字典中的数据
        strSql = String.Format("Delete From tbccDictory Where projectID='{0}'", pi.ProjectID)
        tic.Add(strSql, Nothing)

        '删除工程图表的数据
        strSql = String.Format("Delete From tbccProjectImages Where projectID='{0}'", pi.ProjectID)
        tic.Add(strSql, Nothing)

        '删除工程图表中显示组件中的数据
        strSql = String.Format("Delete From tbccImageControls Where projectID='{0}'", pi.ProjectID)
        tic.Add(strSql, Nothing)

        '删除工程类型数据
        strSql = String.Format("Delete From TbccDevType Where projectID='{0}'", pi.ProjectID)
        tic.Add(strSql, Nothing)

        '删除工程冷库信息数据
        strSql = String.Format("Delete From TbccRefInfo Where projectID='{0}'", pi.ProjectID)
        tic.Add(strSql, Nothing)

        '删除工程AI信息数据
        strSql = String.Format("Delete From TbccAiInfo Where projectID='{0}'", pi.ProjectID)
        tic.Add(strSql, Nothing)

        '删除工程DI/DO信息数据
        strSql = String.Format("Delete From TbccDiDoInfo Where projectID='{0}'", pi.ProjectID)
        tic.Add(strSql, Nothing)

        '删除工程设备自检数据
        strSql = String.Format("Delete From tbccCheckAlarm Where projectID='{0}'", pi.ProjectID)
        tic.Add(strSql, Nothing)

        '删除参数校准数据
        strSql = String.Format("Delete From tbccCalibLog Where projectID='{0}'", pi.ProjectID)
        tic.Add(strSql, Nothing)

        '删除终端启停记录表
        strSql = String.Format("Drop TABLE TbccHDStartUp_{0}", pid_str)
        tic.Add(strSql, Nothing)

        '删除终端启停记录明细表
        strSql = String.Format("Drop TABLE TbccHDStartUpDetail_{0}", pid_str)
        tic.Add(strSql, Nothing)

        '删除终端历史数据记录表
        'For Each dti As DevTypeItem In pi.DevTypeGroupList
        For i As Integer = 1 To netcount
            strSql = String.Format("Drop TABLE TbccHDData_{0}_{1}", pid_str, i)
            tic.Add(strSql, Nothing)
        Next



        '增加工程信息到数据库tbccProject
        strSql = String.Format("Insert Into tbccProject(ProjectID,ProjectName,listNo,remark,phoneNumber,ConnectType," _
                & "ComNumber,DevCount,RefCount,imageCount,AuthCode,ProjectType,ProjectMacID) Values('{0}','{1}',{2},'{3}','{4}',{5},{6},{7},{8},{9},'{10}',{11},'{12}')", _
                pi.ProjectID, pi.ProjectName, pi.listNo, pi.Remark, pi.Phone, pi.ConnectType, pi.ComNumber, pi.DevCount, pi.RefCount, pi.imageList.Count, pi.AuthCode, pi.ProjectType, pi.ProjectMacID)
        tic.Add(strSql, Nothing)

        '工程字典数据TbccDictory
        For Each item As KeyValueItem In pi.DictoryItem.DicList
            strSql = String.Format("Insert into TbccDictory(projectID,key,value) Values('{0}','{1}','{2}')", pi.ProjectID, item.key, item.value)
            tic.Add(strSql, Nothing)
        Next

        For Each pii As ProjectImageItem In pi.imageList
            '增加工程图表信息tbccProjectImages
            strSql = String.Format("Insert Into tbccProjectImages(imageID,projectID,Imagename,listNo,imagePath) Values('{0}','{1}','{2}',{3},'{4}')", pii.ImageID, pii.projectID, pii.imageName, pii.ImageOrder, pii.imagePath)
            tic.Add(strSql, Nothing)

            For Each ici As ImgControlItem In pii.ImgControlItemGroupList
                '增加总工程图中显示组件数据tbccImageControls
                strSql = String.Format("Insert Into tbccImageControls(ProjectID,Netid,Refid,DataType,PortNo,ImageID,TitleMsg,TipMsg,UnitMsg," _
                & "cLeft,cTop,cWidth,cHeight) Values('{0}',{1},{2},{3},{4},'{5}','{6}','{7}','{8}',{9},{10},{11},{12})", ici.ProjectID, ici.Netid, ici.Refid, ici.DataType, ici.PortNo, ici.ImageID, ici.TitleMsg, ici.TipMsg, ici.UnitMsg, ici.cLeft, ici.cTop, ici.cWidth, ici.cHeight)
                strSql &= " --" & iTemp
                iTemp += 1
                tic.Add(strSql, Nothing)
            Next
        Next

        '创建终端启停记录表
        strSql = String.Format("CREATE TABLE 'TbccHDStartUp_{0}'([ID] integer PRIMARY KEY ASC AUTOINCREMENT,[NetID] integer" _
                            & ",[BeginTime] datetime,[EndTime] datetime,[PageIndex] integer,[PageCount] integer,[RecordInterval] integer,[LastPageIndex] integer,[LastPageType] integer" _
                            & ",[BeginAddress] text,[EndAddress] text,[Shipper] text,[Carrier] text,[Receiver] text,[UploadStatus] integer,[LastRecordTime] datetime)", pid_str)
        tic.Add(strSql, Nothing)

        '创建终端启停记录表索引
        strSql = String.Format("CREATE  INDEX MAIN.[Index_HDStartUp_{0}] On [TbccHDStartUp_{0}] ([BeginTime] ,[EndTime] )", pid_str)
        tic.Add(strSql, Nothing)

        '创建终端启停记录明细表
        strSql = String.Format("CREATE TABLE 'TbccHDStartUpDetail_{0}'([ID] integer PRIMARY KEY ASC AUTOINCREMENT,[ParentID] integer,[NetID] integer" _
                            & ",[BeginTime] datetime,[EndTime] datetime,[UploadStatus] integer,[LastRecordTime] datetime,[LastPageIndex] integer,[LastPageType] integer" _
                            & ",[BeginAddress] text,[EndAddress] text,[Shipper] text,[Carrier] text,[Receiver] text)", pid_str)
        tic.Add(strSql, Nothing)

        '创建终端启停记录明细表索引
        strSql = String.Format("CREATE  INDEX MAIN.[Index_HDStartUpDetail_{0}] On [TbccHDStartUpDetail_{0}] ([BeginTime] ,[EndTime] )", pid_str)
        tic.Add(strSql, Nothing)

        For Each dti As DevTypeItem In pi.DevTypeGroupList
            '工程类型数据TbccDevType
            strSql = String.Format("Insert into TbccDevType(projectID,Netid,DevType,RefNum) Values('{0}',{1},{2},{3})", dti.ProjectID, dti.NetId, dti.DevType, dti.RefNum)
            tic.Add(strSql, Nothing)

            For Each rti As RefTypeItem In dti.RefTypeGroupList
                '冷库信息TbccRefInfo
                strSql = String.Format("Insert into TbccRefInfo(projectID,Netid,Refid,RealRefId,RefName,HDCurveTempUp,HDCurveTempDown,HDCurveHumiUp,HDCurveHumiDown,FloorType,FloorNo,RefType) Values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}')", rti.ProjectID, rti.NetId, rti.RefId, rti.RealRefId, rti.RefName, rti.HDCurveTempUp, rti.HDCurveTempDown, rti.HDCurveHumiUp, rti.HDCurveHumiDown, rti.FloorType, rti.FloorNo, rti.RefType)
                tic.Add(strSql, Nothing)

                'AI端口TbccAiInfo
                For Each apti As AiPortTypeItem In rti.AiPortTypeGroupList
                    strSql = String.Format("Insert Into TbccAiInfo(projectID,Netid,Refid,SeqPortNo,PortNo,PortName,DataType) Values('{0}',{1},{2},{3},{4},'{5}',{6})", apti.ProjectID, apti.NetId, apti.RefId, apti.SeqPortNo, apti.PortNo, apti.PortName, apti.DataType)
                    strSql &= " --" & iTemp
                    iTemp += 1
                    tic.Add(strSql, Nothing)
                Next

                'DI/DO端口激活信息TbccDiDoInfo
                For Each diopti As DiDoPortTypeItem In rti.DiDoPortTypeGroupList
                    strSql = String.Format("Insert Into TbccDiDoInfo(projectID,Netid,Refid,SeqPortNo,PortNo,PortName,DataType,PortType) Values('{0}',{1},{2},{3},{4},'{5}',{6},{7}) ", diopti.ProjectID, diopti.NetId, diopti.RefId, diopti.SeqPortNo, diopti.PortNo, diopti.PortName, diopti.DataType, diopti.PortType)
                    strSql &= " --" & iTemp
                    iTemp += 1
                    tic.Add(strSql, Nothing)
                Next

            Next

            '创建终端历史数据记录表
            strSql = String.Format("CREATE TABLE 'TbccHDData_{0}_{1}'([ID] integer PRIMARY KEY ASC AUTOINCREMENT,[ParentID] integer,[NetID] integer" _
                                & ",[RecordTime] datetime", pid_str, dti.NetId)
            Select Case pi.ProjectType
                Case ProjectType_Enum.Car
                    strSql &= ",[AI1] float,[AI2] Float,[AI3] float,[AI4] float,[Longitude_Dir] integer,[Longitude] float,[Latitude_Dir] integer,[Latitude] float)"
                Case ProjectType_Enum.Box
                    strSql &= ",[AI1] float,[Longitude_Dir] integer,[Longitude] float,[Latitude_Dir] integer,[Latitude] float)"
                Case Else
                    strSql &= ",[AI1] float,[AI2] Float,[AI3] float,[AI4] float,[Longitude_Dir] integer,[Longitude] float,[Latitude_Dir] integer,[Latitude] float)"
            End Select
            tic.Add(strSql, Nothing)

            '创建终端历史数据记录表索引
            strSql = String.Format("CREATE INDEX [Index_HDData_{0}_{1}] On [TbccHDData_{0}_{1}] ([RecordTime] )", pid_str, dti.NetId)
            tic.Add(strSql, Nothing)
        Next

        Return SqlHelper.ExecuteTranstration(tic)

    End Function

    Public Shared Function Delete(ByVal pi As ProjectItem, ByVal netcount As Integer) As Integer

        Dim strSql As String
        Dim pid_str As String
        pid_str = Trim(pi.ProjectID).Replace("-", "_")
        Dim tic As New TBCC.UtilityMobile.TranstrationItem

        '删除工程信息总表中的数据
        strSql = String.Format("Delete From tbccProject Where projectID='{0}'", pi.ProjectID)
        tic.Add(strSql, Nothing)

        '删除工程信息字典中的数据
        strSql = String.Format("Delete From tbccDictory Where projectID='{0}'", pi.ProjectID)
        tic.Add(strSql, Nothing)

        '删除工程图表的数据
        strSql = String.Format("Delete From tbccProjectImages Where projectID='{0}'", pi.ProjectID)
        tic.Add(strSql, Nothing)

        '删除工程图表中显示组件中的数据
        strSql = String.Format("Delete From tbccImageControls Where projectID='{0}'", pi.ProjectID)
        tic.Add(strSql, Nothing)

        '删除工程类型数据
        strSql = String.Format("Delete From TbccDevType Where projectID='{0}'", pi.ProjectID)
        tic.Add(strSql, Nothing)

        '删除工程冷库信息数据
        strSql = String.Format("Delete From TbccRefInfo Where projectID='{0}'", pi.ProjectID)
        tic.Add(strSql, Nothing)

        '删除工程AI信息数据
        strSql = String.Format("Delete From TbccAiInfo Where projectID='{0}'", pi.ProjectID)
        tic.Add(strSql, Nothing)

        '删除工程DI/DO信息数据
        strSql = String.Format("Delete From TbccDiDoInfo Where projectID='{0}'", pi.ProjectID)
        tic.Add(strSql, Nothing)

        '删除设备自检数据
        strSql = String.Format("Delete From tbccCheckAlarm Where projectID='{0}'", pi.ProjectID)
        tic.Add(strSql, Nothing)

        '删除参数校准数据
        strSql = String.Format("Delete From tbccCalibLog Where projectID='{0}'", pi.ProjectID)
        tic.Add(strSql, Nothing)

        '删除终端启停记录表
        strSql = String.Format("Drop TABLE TbccHDStartUp_{0}", pid_str)
        tic.Add(strSql, Nothing)

        '删除终端启停记录明细表
        strSql = String.Format("Drop TABLE TbccHDStartUpDetail_{0}", pid_str)
        tic.Add(strSql, Nothing)

        '删除终端历史数据记录表
        For i As Integer = 1 To netcount
            strSql = String.Format("Drop TABLE TbccHDData_{0}_{1}", pid_str, i)
            tic.Add(strSql, Nothing)
        Next

        Return SqlHelper.ExecuteTranstration(tic)

    End Function

    Public Shared Function GetProjectList() As IList(Of ProjectItem)
        Dim pl As IList(Of ProjectItem)
        pl = New List(Of ProjectItem)

        Dim strSql As String
        strSql = "Select * From tbccProject;Select * From tbccProjectImages;Select * From tbccImageControls;Select * From TbccDevType Order By Netid;Select * From TbccRefInfo;Select * From TbccDiDoInfo;Select * From TbccAiInfo;Select * From TbccDictory"

        Dim ds As DataSet
        ds = TBCC.UtilityMobile.SqlHelper.ExecuteDataset(strSql, Nothing)

        '查询错误
        If ds Is Nothing OrElse ds.Tables.Count = 0 Then
            Return Nothing
        End If

        Dim dtProject As DataTable
        dtProject = ds.Tables(0)

        Dim dtImage As DataTable
        dtImage = ds.Tables(1)

        dtProject.Columns.Add("iSort", GetType(Integer))
        For Each r As DataRow In dtProject.Rows
            r("iSort") = r("listNo")
        Next

        dtProject.DefaultView.Sort = "iSort asc"

        For Each r As DataRowView In dtProject.DefaultView
            Dim pi As New ProjectItem
            pi.ProjectID = r("projectID")
            pi.Phone = Convert.ToString(r("phoneNumber")).Trim()
            pi.ProjectName = r("ProjectName")
            pi.listNo = r("listNo")
            pi.Remark = Convert.ToString(r("Remark"))
            pi.ComNumber = r("ComNumber")
            pi.connectType = r("ConnectType")
            'pi.DevCount = r("DevCount")
            'pi.RefCount = r("RefCount")
            pi.AuthCode = r("AuthCode").ToString()
            pi.ProjectType = r("ProjectType")
            'pi.ProjectMacID = r("ProjectMacID").ToString()


            Dim filter4 As String
            filter4 = String.Format("ProjectID='{0}'", pi.ProjectID)

            '工程字典信息

            Dim pdi As New ProjectDictoryItem()
            For Each r8 As DataRow In ds.Tables(7).Select(filter4) '当前字典每一个key&value
                Dim kvi As KeyValueItem = New KeyValueItem(r8("key"), r8("value"))
                pdi.DicList.Add(kvi)
            Next
            pi.DictoryItem = pdi

            Dim filter As String
            filter = String.Format("ProjectID='{0}'", pi.ProjectID)

            '*******************************************
            '图层增加排序 20080225
            Dim dv2 As New DataView(dtImage, filter, "listNo", DataViewRowState.CurrentRows)
            For Each r2 As DataRowView In dv2
                Dim pii As New ProjectImageItem()
                pii.projectID = pi.ProjectID
                pii.imageName = r2("imageName")
                pii.ImageOrder = r2("listNo")
                pii.imagePath = r2("imagePath")
                pii.ImageID = r2("imageID")

                Dim filter1 As String
                filter1 = String.Format("ProjectID='{0}' and ImageID='{1}'", pi.ProjectID, pii.ImageID)

                For Each r3 As DataRow In ds.Tables(2).Select(filter1) '当前图层对应的每一个显示标签
                    Dim ici As New ImgControlItem()
                    ici.cHeight = r3("cHeight")
                    ici.cLeft = r3("cLeft")
                    ici.cTop = r3("cTop")
                    ici.cWidth = r3("cWidth")
                    ici.ImageID = r3("ImageID")
                    ici.Netid = r3("Netid")
                    ici.PortNo = r3("PortNo")
                    ici.DataType = r3("DataType")
                    ici.ProjectID = r3("ProjectID")
                    ici.Refid = r3("Refid")
                    ici.TipMsg = r3("TipMsg")
                    ici.TitleMsg = r3("TitleMsg")
                    ici.UnitMsg = r3("UnitMsg")
                    pii.ImgControlItemGroupList.Add(ici)
                Next

                pi.imageList.Add(pii)
            Next

            '*******************************************

            '工程类型信息
            For Each r4 As DataRow In ds.Tables(3).Select(filter) '对每一个设备
                Dim dti As New DevTypeItem()
                dti.ProjectID = r4("projectID")
                dti.NetId = r4("Netid")
                dti.DevType = r4("DevType")
                dti.RefNum = r4("RefNum")

                Dim filter2 As String
                filter2 = String.Format("ProjectID='{0}' and Netid='{1}'", pi.ProjectID, dti.NetId)

                For Each r5 As DataRow In ds.Tables(4).Select(filter2) '对每一个冷库
                    Dim rti As New RefTypeItem()
                    rti.ProjectID = r5("projectID")
                    rti.NetId = r5("Netid")
                    rti.RefId = r5("Refid")
                    rti.RealRefId = r5("RealRefId")
                    rti.RefName = r5("RefName")
                    rti.FloorType = r5("FloorType")
                    rti.FloorNo = r5("FloorNo")
                    rti.RefType = r5("RefType")
                    rti.HDCurveTempUp = r5("HDCurveTempUp")
                    rti.HDCurveTempDown = r5("HDCurveTempDown")
                    rti.HDCurveHumiUp = r5("HDCurveHumiUp")
                    rti.HDCurveHumiDown = r5("HDCurveHumiDown")

                    Dim filter3 As String
                    filter3 = String.Format("ProjectID='{0}' and Netid='{1}' and Refid='{2}'", pi.ProjectID, dti.NetId, rti.RefId)

                    For Each r6 As DataRow In ds.Tables(5).Select(filter3) '对该冷库每一个DI/DO
                        Dim didopti As New DiDoPortTypeItem()
                        didopti.ProjectID = r6("projectID")
                        didopti.NetId = r6("Netid")
                        didopti.RefId = r6("Refid")
                        didopti.PortType = r6("PortType")
                        didopti.PortName = r6("PortName")
                        didopti.SeqPortNo = r6("SeqPortNo")
                        didopti.PortNo = r6("PortNo")
                        didopti.DataType = r6("DataType")
                        rti.DiDoPortTypeGroupList.Add(didopti)
                    Next


                    For Each r7 As DataRow In ds.Tables(6).Select(filter3) '对该冷库每一个AI
                        Dim aipti As New AiPortTypeItem()
                        aipti.ProjectID = r7("ProjectID")
                        aipti.NetId = r7("Netid")
                        aipti.RefId = r7("Refid")
                        aipti.SeqPortNo = r7("SeqPortNo")
                        aipti.PortNo = r7("PortNo")
                        aipti.PortName = r7("PortName")
                        aipti.DataType = r7("DataType")
                        rti.AiPortTypeGroupList.Add(aipti)
                    Next

                    dti.RefTypeGroupList.Add(rti)
                Next

                pi.DevTypeGroupList.Add(dti)
            Next

            pl.Add(pi)
        Next

        Return pl

    End Function


    '刷新当前工程所有信息
    Public Shared Function RefreshCurProjectList(ByVal projectid As String) As ProjectItem

        Dim pi As ProjectItem = Nothing

        Dim strSql As String
        strSql = "Select * From tbccProject;Select * From tbccProjectImages;Select * From tbccImageControls;Select * From TbccDevType Order By Netid;Select * From TbccRefInfo;Select * From TbccDiDoInfo;Select * From TbccAiInfo;Select * From tbccDictory"

        Dim ds As DataSet
        ds = TBCC.UtilityMobile.SqlHelper.ExecuteDataset(strSql, Nothing)

        '查询错误
        If ds Is Nothing OrElse ds.Tables.Count = 0 Then
            Return Nothing
        End If

        Dim dtProject As DataTable
        dtProject = ds.Tables(0)

        Dim dtImage As DataTable
        dtImage = ds.Tables(1)

        dtProject.Columns.Add("iSort", GetType(Integer))
        For Each r As DataRow In dtProject.Rows
            r("iSort") = r("listNo")
        Next

        dtProject.DefaultView.Sort = "iSort asc"

        For Each r As DataRowView In dtProject.DefaultView
            If r("projectID") <> projectid Then
                Continue For
            End If

            pi = New ProjectItem

            pi.ProjectID = r("projectID")
            pi.Phone = Convert.ToString(r("phoneNumber")).Trim()
            pi.ProjectName = r("ProjectName")
            pi.listNo = r("listNo")
            pi.Remark = Convert.ToString(r("Remark"))
            pi.ComNumber = r("ComNumber")
            pi.connectType = r("ConnectType")
            pi.DevCount = r("DevCount")
            pi.RefCount = r("RefCount")
            pi.AuthCode = r("AuthCode").ToString()
            pi.ProjectType = r("ProjectType")
            pi.ProjectMacID = r("ProjectMacID").ToString()


            '工程字典信息
            Dim filter4 As String
            filter4 = String.Format("ProjectID='{0}'", pi.ProjectID)

            Dim pdi As New ProjectDictoryItem()
            For Each r8 As DataRow In ds.Tables(7).Select(filter4) '当前字典每一个key&value
                Dim kvi As KeyValueItem = New KeyValueItem(r8("key"), r8("value"))
                pdi.DicList.Add(kvi)
            Next
            pi.DictoryItem = pdi

            Dim filter As String
            filter = String.Format("ProjectID='{0}'", pi.ProjectID)

            '*******************************************
            '图层增加排序 
            Dim dv2 As New DataView(dtImage, filter, "listNo", DataViewRowState.CurrentRows)
            For Each r2 As DataRowView In dv2
                Dim pii As New ProjectImageItem()
                pii.projectID = pi.ProjectID
                pii.imageName = r2("imageName")
                pii.ImageOrder = r2("listNo")
                pii.imagePath = r2("imagePath")
                pii.ImageID = r2("imageID")

                Dim filter1 As String
                filter1 = String.Format("ProjectID='{0}' and ImageID='{1}'", pi.ProjectID, pii.ImageID)

                For Each r3 As DataRow In ds.Tables(2).Select(filter1) '当前图层对应的每一个显示标签
                    Dim ici As New ImgControlItem()
                    ici.cHeight = r3("cHeight")
                    ici.cLeft = r3("cLeft")
                    ici.cTop = r3("cTop")
                    ici.cWidth = r3("cWidth")
                    ici.ImageID = r3("ImageID")
                    ici.Netid = r3("Netid")
                    ici.PortNo = r3("PortNo")
                    ici.DataType = r3("DataType")
                    ici.ProjectID = r3("ProjectID")
                    ici.Refid = r3("Refid")
                    ici.TipMsg = r3("TipMsg")
                    ici.TitleMsg = r3("TitleMsg")
                    ici.UnitMsg = r3("UnitMsg")
                    pii.ImgControlItemGroupList.Add(ici)
                Next

                pi.imageList.Add(pii)
            Next

            '*******************************************

            '工程类型信息
            For Each r4 As DataRow In ds.Tables(3).Select(filter) '对每一个设备
                Dim dti As New DevTypeItem()
                dti.ProjectID = r4("projectID")
                dti.NetId = r4("Netid")
                dti.DevType = r4("DevType")
                dti.RefNum = r4("RefNum")

                Dim filter2 As String
                filter2 = String.Format("ProjectID='{0}' and Netid='{1}'", pi.ProjectID, dti.NetId)

                For Each r5 As DataRow In ds.Tables(4).Select(filter2) '对每一个冷库
                    Dim rti As New RefTypeItem()
                    rti.ProjectID = r5("projectID")
                    rti.NetId = r5("Netid")
                    rti.RefId = r5("Refid")
                    rti.RealRefId = r5("RealRefId")
                    rti.RefName = r5("RefName")
                    rti.FloorType = r5("FloorType")
                    rti.FloorNo = r5("FloorNo")
                    rti.RefType = r5("RefType")
                    rti.HDCurveTempUp = r5("HDCurveTempUp")
                    rti.HDCurveTempDown = r5("HDCurveTempDown")
                    rti.HDCurveHumiUp = r5("HDCurveHumiUp")
                    rti.HDCurveHumiDown = r5("HDCurveHumiDown")

                    Dim filter3 As String
                    filter3 = String.Format("ProjectID='{0}' and Netid='{1}' and Refid='{2}'", pi.ProjectID, dti.NetId, rti.RefId)

                    For Each r6 As DataRow In ds.Tables(5).Select(filter3) '对该冷库每一个DI/DO
                        Dim didopti As New DiDoPortTypeItem()
                        didopti.ProjectID = r6("projectID")
                        didopti.NetId = r6("Netid")
                        didopti.RefId = r6("Refid")
                        didopti.PortType = r6("PortType")
                        didopti.PortName = r6("PortName")
                        didopti.SeqPortNo = r6("SeqPortNo")
                        didopti.PortNo = r6("PortNo")
                        didopti.DataType = r6("DataType")
                        rti.DiDoPortTypeGroupList.Add(didopti)
                    Next


                    For Each r7 As DataRow In ds.Tables(6).Select(filter3) '对该冷库每一个AI
                        Dim aipti As New AiPortTypeItem()
                        aipti.ProjectID = r7("ProjectID")
                        aipti.NetId = r7("Netid")
                        aipti.RefId = r7("Refid")
                        aipti.SeqPortNo = r7("SeqPortNo")
                        aipti.PortNo = r7("PortNo")
                        aipti.PortName = r7("PortName")
                        aipti.DataType = r7("DataType")
                        rti.AiPortTypeGroupList.Add(aipti)
                    Next

                    dti.RefTypeGroupList.Add(rti)
                Next

                pi.DevTypeGroupList.Add(dti)
            Next
        Next

        Return pi

    End Function


#Region "设备类型配置功能"
    '为设备类型配置提供获取当前设备和冷库名称等信息的功能
    Public Shared Function GetDevRefTypeList(ByVal pi As ProjectItem) As IList(Of DevTypeItem)
        Dim dtil As IList(Of DevTypeItem)
        dtil = New List(Of DevTypeItem)

        Dim strSql As String
        strSql = "Select * From TbccDevType order by Netid;Select * From TbccRefInfo order by Netid"

        Dim ds As DataSet
        ds = TBCC.UtilityMobile.SqlHelper.ExecuteDataset(strSql, Nothing)

        '查询错误
        If ds Is Nothing OrElse ds.Tables.Count = 0 Then
            Return Nothing
        End If

        Dim filter1 As String
        filter1 = String.Format("ProjectID='{0}'", pi.ProjectID)

        For Each r1 As DataRow In ds.Tables(0).Select(filter1) '对每一个设备
            Dim dti As New DevTypeItem()
            dti.ProjectID = r1("projectID")
            dti.NetId = r1("Netid")
            dti.DevType = r1("DevType")
            dti.RefNum = r1("RefNum")

            Dim filter2 As String
            filter2 = String.Format("ProjectID='{0}' and Netid='{1}'", pi.ProjectID, dti.NetId)

            For Each r2 As DataRow In ds.Tables(1).Select(filter2) '对每一个冷库
                Dim rti As New RefTypeItem()
                rti.ProjectID = r2("projectID")
                rti.RefName = r2("Netid")
                rti.RefId = r2("Refid")
                rti.RealRefId = r2("RealRefId")
                rti.RefName = r2("RefName")
                rti.FloorType = r2("FloorType")
                rti.FloorNo = r2("FloorNo")
                rti.RefType = r2("RefType")
                rti.HDCurveTempUp = r2("HDCurveTempUp")
                rti.HDCurveTempDown = r2("HDCurveTempDown")
                rti.HDCurveHumiUp = r2("HDCurveHumiUp")
                rti.HDCurveHumiDown = r2("HDCurveHumiDown")

                dti.RefTypeGroupList.Add(rti)
            Next

            dtil.Add(dti)
        Next

        Return dtil

    End Function

    'ok
    '为设备类型配置提供增加功能 
    'netcount:当前工程已经配置的网络设备的数量
    Public Shared Function AddDevRefTypeList(ByVal item As DevTypeItem, ByVal netcount As Integer, ByVal prjType As ProjectType_Enum) As Boolean
        Dim strSql As String
        Dim pid_str As String
        Dim index As Integer = 0

        pid_str = Trim(item.ProjectID).Replace("-", "_")


        strSql = "Insert into TbccDevType(projectID,Netid,DevType,RefNum) Values(?,?,?,?)"

        Dim p1(3) As System.Data.Common.DbParameter

        p1(0) = New MySql.Data.MySqlClient.MySqlParameter()
        p1(0).Value = item.ProjectID

        p1(1) = New MySql.Data.MySqlClient.MySqlParameter()
        p1(1).Value = item.NetId

        p1(2) = New MySql.Data.MySqlClient.MySqlParameter()
        p1(2).Value = item.DevType

        p1(3) = New MySql.Data.MySqlClient.MySqlParameter()
        p1(3).Value = item.RefNum

        Dim tic As New TBCC.UtilityMobile.TranstrationItem
        tic.Add(strSql, p1)

        For Each rti As RefTypeItem In item.RefTypeGroupList
            strSql = String.Format("Insert into TbccRefInfo(projectID,Netid,Refid,RealRefId,RefName,HDCurveTempUp,HDCurveTempDown,HDCurveHumiUp,HDCurveHumiDown,FloorType,FloorNo,RefType) Values('{0}',{1},{2},{3},'{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}')", item.ProjectID, item.NetId, rti.RefId, rti.RealRefId, rti.RefName, rti.HDCurveTempUp, rti.HDCurveTempDown, rti.HDCurveHumiUp, rti.HDCurveHumiDown, rti.FloorType, rti.FloorNo, rti.RefType)
            tic.Add(strSql, Nothing)

            For Each apti As AiPortTypeItem In rti.AiPortTypeGroupList
                strSql = String.Format("Insert Into TbccAiInfo(projectID,Netid,Refid,SeqPortNo,PortNo,PortName,DataType) Values('{0}',{1},{2},{3},{4},'{5}',{6})", apti.ProjectID, apti.NetId, apti.RefId, apti.SeqPortNo, apti.PortNo, apti.PortName, apti.DataType)
                tic.Add(strSql, Nothing)
            Next

            For Each diopti As DiDoPortTypeItem In rti.DiDoPortTypeGroupList
                strSql = String.Format("Insert Into TbccDiDoInfo(projectID,Netid,Refid,SeqPortNo,PortNo,PortName,DataType,PortType) Values('{0}',{1},{2},{3},{4},'{5}',{6},{7})", diopti.ProjectID, diopti.NetId, diopti.RefId, diopti.SeqPortNo, diopti.PortNo, diopti.PortName, diopti.DataType, diopti.PortType)
                tic.Add(strSql, Nothing)
            Next
        Next

        '创建终端历史数据记录表
        strSql = String.Format("CREATE TABLE 'TbccHDData_{0}_{1}'([ID] integer PRIMARY KEY ASC AUTOINCREMENT,[ParentID] integer,[NetID] integer" _
                            & ",[RecordTime] datetime", pid_str, item.NetId)
        Select Case prjType
            Case ProjectType_Enum.Car
                strSql &= ",[AI1] float,[AI2] Float,[AI3] float,[AI4] float,[Longitude_Dir] integer,[Longitude] float,[Latitude_Dir] integer,[Latitude] float)"
            Case ProjectType_Enum.Box
                strSql &= ",[AI1] float,[Longitude_Dir] integer,[Longitude] float,[Latitude_Dir] integer,[Latitude] float)"
            Case Else
                strSql &= ",[AI1] float,[AI2] Float,[AI3] float,[AI4] float,[Longitude_Dir] integer,[Longitude] float,[Latitude_Dir] integer,[Latitude] float)"
        End Select
        tic.Add(strSql, Nothing)

        '创建终端历史数据记录表索引
        strSql = String.Format("CREATE INDEX [Index_HDData_{0}_{1}] On [TbccHDData_{0}_{1}] ([RecordTime] )", pid_str, item.NetId)
        tic.Add(strSql, Nothing)

        Return TBCC.UtilityMobile.SqlHelper.ExecuteTranstration(tic)

    End Function

    '为设备类型配置提供更新功能
    Public Shared Function UpdateDevRefTypeList(ByVal item As DevTypeItem, ByVal pi As ProjectItem) As Boolean
        Dim pid_str As String
        pid_str = Trim(pi.ProjectID).Replace("-", "_")
        Dim strSql As String
        Dim index As Integer
        Dim tic As New TBCC.UtilityMobile.TranstrationItem

        strSql = String.Format("delete from TbccDevType where projectID='{0}' and Netid='{1}' --" & index, item.ProjectID, item.NetId)
        tic.Add(strSql, Nothing)
        index += 1

        strSql = "Insert into TbccDevType(projectID,Netid,DevType,RefNum) Values(?,?,?,?) --" & index

        Dim p1(3) As System.Data.Common.DbParameter

        p1(0) = New MySql.Data.MySqlClient.MySqlParameter()
        p1(0).Value = item.ProjectID

        p1(1) = New MySql.Data.MySqlClient.MySqlParameter()
        p1(1).Value = item.NetId

        p1(2) = New MySql.Data.MySqlClient.MySqlParameter()
        p1(2).Value = item.DevType

        p1(3) = New MySql.Data.MySqlClient.MySqlParameter()
        p1(3).Value = item.RefNum

        tic.Add(strSql, p1)
        index += 1


        strSql = String.Format("delete from TbccRefInfo where projectID='{0}' and Netid='{1}' --" & index, item.ProjectID, item.NetId)
        tic.Add(strSql, Nothing)
        index += 1

        strSql = String.Format("delete from TbccAiInfo where projectID='{0}' and Netid='{1}' --" & index, item.ProjectID, item.NetId)
        tic.Add(strSql, Nothing)
        index += 1

        strSql = String.Format("delete from TbccDiDoInfo where projectID='{0}' and Netid='{1}' --" & index, item.ProjectID, item.NetId)
        tic.Add(strSql, Nothing)
        index += 1

        strSql = String.Format("delete from tbccImageControls where projectID='{0}' and Netid='{1}' --" & index, item.ProjectID, item.NetId)
        tic.Add(strSql, Nothing)
        index += 1


        strSql = "Insert into TbccRefInfo(projectID,Netid,Refid,RealRefId,RefName,HDCurveTempUp,HDCurveTempDown,HDCurveHumiUp,HDCurveHumiDown,FloorType,FloorNo,RefType) Values(?,?,?,?,?,?,?,?,?,?,?,?) --"


        For Each rti As RefTypeItem In item.RefTypeGroupList
            Dim p2(11) As System.Data.Common.DbParameter

            For i As Integer = 0 To p2.GetUpperBound(0)
                p2(i) = New MySql.Data.MySqlClient.MySqlParameter()
            Next

            p2(0).Value = item.ProjectID
            p2(1).Value = item.NetId
            p2(2).Value = rti.RefId
            p2(3).Value = rti.RealRefId
            p2(4).Value = rti.RefName
            p2(5).Value = rti.HDCurveTempUp
            p2(6).Value = rti.HDCurveTempDown
            p2(7).Value = rti.HDCurveHumiUp
            p2(8).Value = rti.HDCurveHumiDown
            p2(9).Value = rti.FloorType
            p2(10).Value = rti.FloorNo
            p2(11).Value = rti.RefType

            tic.Add(strSql & index, p2)
            index += 1

            For Each apti As AiPortTypeItem In rti.AiPortTypeGroupList
                strSql = String.Format("Insert Into TbccAiInfo(projectID,Netid,Refid,SeqPortNo,PortNo,PortName,DataType) Values('{0}',{1},{2},{3},{4},'{5}',{6}) --" & index, apti.ProjectID, apti.NetId, apti.RefId, apti.SeqPortNo, apti.PortNo, apti.PortName, apti.DataType)
                tic.Add(strSql, Nothing)
                index += 1
            Next

            For Each diopti As DiDoPortTypeItem In rti.DiDoPortTypeGroupList
                strSql = String.Format("Insert Into TbccDiDoInfo(projectID,Netid,Refid,SeqPortNo,PortNo,PortName,DataType,PortType) Values('{0}',{1},{2},{3},{4},'{5}',{6},{7}) --" & index, diopti.ProjectID, diopti.NetId, diopti.RefId, diopti.SeqPortNo, diopti.PortNo, diopti.PortName, diopti.DataType, diopti.PortType)
                tic.Add(strSql, Nothing)
                index += 1
            Next
        Next

        '删除终端历史数据记录表
        strSql = String.Format("Drop TABLE TbccHDData_{0}_{1}", pid_str, item.NetId)
        tic.Add(strSql, Nothing)


        '创建终端历史数据记录表
        strSql = String.Format("CREATE TABLE 'TbccHDData_{0}_{1}'([ID] integer PRIMARY KEY ASC AUTOINCREMENT,[ParentID] integer,[NetID] integer" _
                            & ",[RecordTime] datetime", pid_str, item.NetId)
        Select Case pi.ProjectType
            Case ProjectType_Enum.Car
                strSql &= ",[AI1] float,[AI2] Float,[AI3] float,[AI4] float,[Longitude_Dir] integer,[Longitude] float,[Latitude_Dir] integer,[Latitude] float)"
            Case ProjectType_Enum.Box
                strSql &= ",[AI1] float,[Longitude_Dir] integer,[Longitude] float,[Latitude_Dir] integer,[Latitude] float)"
            Case Else
                strSql &= ",[AI1] float,[AI2] Float,[AI3] float,[AI4] float,[Longitude_Dir] integer,[Longitude] float,[Latitude_Dir] integer,[Latitude] float)"
        End Select
        tic.Add(strSql, Nothing)

        '创建终端历史数据记录表索引
        strSql = String.Format("CREATE INDEX [Index_HDData_{0}_{1}] On [TbccHDData_{0}_{1}] ([RecordTime] )", pid_str, item.NetId)
        tic.Add(strSql, Nothing)

        Return TBCC.UtilityMobile.SqlHelper.ExecuteTranstration(tic)

    End Function
    '为设备类型配置提供删除功能
    'netcount:剩余的设备的数量
    Public Shared Function DelDevRefTypeList(ByVal item As DevTypeItem, ByVal netcount As Integer) As Boolean
        Dim strSql As String
        Dim pid_str As String
        Dim tic As New TBCC.UtilityMobile.TranstrationItem

        pid_str = Trim(item.ProjectID).Replace("-", "_")

        '删除设备类型表的数据
        strSql = String.Format("delete from TbccDevType where projectID='{0}' and Netid={1}", item.ProjectID, item.NetId)
        tic.Add(strSql, Nothing)

        '删除冷库表的数据
        strSql = String.Format("delete from TbccRefInfo where projectID='{0}' and Netid={1}", item.ProjectID, item.NetId)
        tic.Add(strSql, Nothing)

        '删除AI端口表的数据
        strSql = String.Format("delete from TbccAiInfo where projectID='{0}' and Netid={1}", item.ProjectID, item.NetId)
        tic.Add(strSql, Nothing)

        '删除DI/DO端口表的数据
        strSql = String.Format("delete from TbccDiDoInfo where projectID='{0}' and Netid={1}", item.ProjectID, item.NetId)
        tic.Add(strSql, Nothing)

        ''删除工程图表的数据
        'strSql = String.Format("Delete From tbccProjectImages Where projectID='{0}'", item.ProjectID)
        'tic.Add(strSql, Nothing)

        '删除工程图表中显示组件中的数据
        strSql = String.Format("Delete From tbccImageControls Where projectID='{0}' and Netid={1}", item.ProjectID, item.NetId)
        tic.Add(strSql, Nothing)

        '删除当前工程某网络的历史数据表
        strSql = String.Format("Drop TABLE TbccHDData_{0}_{1}", pid_str, item.NetId)
        tic.Add(strSql, Nothing)

        Return TBCC.UtilityMobile.SqlHelper.ExecuteTranstration(tic)

    End Function

    ''' <summary>
    ''' 判断工程中是否包含指定的网络地址
    ''' </summary>
    ''' <param name="netID"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function DevTypeExistNetid(ByVal pi As String, ByVal netID As Byte) As Boolean
        Dim strSql As String
        strSql = "Select count(*) from tbccDevType where projectID=? and Netid =?"

        Dim p(1) As MySql.Data.MySqlClient.MySqlParameter
        p(0) = New MySql.Data.MySqlClient.MySqlParameter
        p(1) = New MySql.Data.MySqlClient.MySqlParameter

        p(0).Value = pi
        p(1).Value = netID

        Dim obj As Object
        obj = SqlHelper.ExecuteScalar(strSql, p)

        If obj = 0 Then
            Return False
        End If

        Return True
    End Function


    ''' <summary>
    ''' 获取当前工程中最大的netid
    ''' </summary>    
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function DevTypeMaxNetid(ByVal pi As String) As Integer
        Dim strSql As String
        strSql = "Select max(Netid) from tbccDevType where projectID=?"

        Dim p(0) As MySql.Data.MySqlClient.MySqlParameter
        p(0) = New MySql.Data.MySqlClient.MySqlParameter
        p(0).Value = pi

        Dim obj As Object
        obj = SqlHelper.ExecuteScalar(strSql, p)

        If obj Is Nothing OrElse IsDBNull(obj) Then
            Return 0
        Else
            Return CInt(obj)
        End If


    End Function


    '根据冷库ID获取某工程和网络的冷库名称
    Public Shared Function GetRefName_FromRefid(ByVal pi As ProjectItem, ByVal netID As Byte, ByVal Refid As Integer) As String
        For Each dti As DevTypeItem In pi.DevTypeGroupList
            If dti.NetId = netID Then
                For Each rti As RefTypeItem In dti.RefTypeGroupList
                    If rti.RefId = Refid Then
                        Return rti.RefName
                    End If
                Next
            End If
        Next
        Return ""
    End Function


    '根据冷库端口编号获取某工程和网络的冷库名称
    Public Shared Function GetRefName_FromPortNo(ByVal pi As ProjectItem, ByVal netID As Byte, ByVal PortNo As Integer) As String
        For Each dti As DevTypeItem In pi.DevTypeGroupList
            If dti.NetId = netID Then
                For Each rti As RefTypeItem In dti.RefTypeGroupList
                    For Each apti As AiPortTypeItem In rti.AiPortTypeGroupList
                        If apti.PortNo = PortNo Then
                            Return rti.RefName
                        End If
                    Next
                Next
            End If
        Next
        Return ""
    End Function

    '根据冷库端口编号获取某工程和网络的该端口名称
    Public Shared Function GetPortName_FromPortNo(ByVal pi As ProjectItem, ByVal netID As Byte, ByVal PortNo As Integer) As String
        For Each dti As DevTypeItem In pi.DevTypeGroupList
            If dti.NetId = netID Then
                For Each rti As RefTypeItem In dti.RefTypeGroupList
                    For Each apti As AiPortTypeItem In rti.AiPortTypeGroupList
                        If apti.PortNo = PortNo Then
                            Return apti.PortName
                        End If
                    Next
                Next
            End If
        Next
        Return ""
    End Function

    '根据冷库编号确定当前DI/DO是否有效
    'm_type: 1: 缺项是否配置; 2: 库门是否配置
    'Public Shared Function GetDioStatus(ByVal pi As ProjectItem, ByVal netID As Byte, ByVal m_type As Integer) As Boolean
    '    For Each dti As DevTypeItem In pi.DevTypeGroupList
    '        If dti.NetId = netID Then
    '            For Each rti As RefTypeItem In dti.RefTypeGroupList

    '                If m_type = 1 Then '缺项是否配置
    '                    Return IIf(rti.DiDoPortInfo.LostPowerPort > 0, True, False)
    '                End If

    '                If m_type = 2 Then '库门是否配置
    '                    Return IIf(rti.DiDoPortInfo.DoorStatusPort > 0, True, False)
    '                End If

    '            Next
    '        End If
    '    Next

    '    Return False

    'End Function

#End Region


End Class
